package cn.szzsi.model;

import cn.szzsi.model.base.BasePlatformResource;
import cn.szzsi.model.base.BaseResource;
import com.jfinal.aop.Interceptor;
import com.jfinal.kit.StrKit;

import java.util.List;

/**
 * 实体类 - 资源
 * 
 */
public class PlatformResource extends BasePlatformResource<PlatformResource> {

	private static final long serialVersionUID = 7906172672735389314L;
	
	public static final PlatformResource dao = new PlatformResource().dao();



	public List<PlatformResource> getByPidAndRoleId(String pid, String roleId){
		if("super".equals(roleId)){
			return dao.find("select * from sys_platform_resource where 1=1 and isShow='1' and parent_id = ? order by orderList asc",pid);
		}else{
			return dao.find("select * from sys_platform_resource where 1=1 and isShow='1' and parent_id = ? and id in(select resourceId from sys_platform_role_resource where roleId=?) order by orderList asc",pid,roleId);
		}

	}

	/**
	 * 根据角色查看资源
	 * @param level
	 * @param roleId
	 * @return
	 */
	public List<PlatformResource>getByLevelAndRoleId(int level,String roleId){
		if("super".equals(roleId)){
			return dao.find("select * from sys_platform_resource where level = ? and isShow='1' order by orderList asc ",level);
		}else{
			return dao.find("select * from sys_platform_resource where level = ? and isShow='1' and id in(select resourceId from sys_platform_role_resource where roleId=?) order by orderList asc",level,roleId);
		}

	}


	public List<PlatformResource> getByLevel(int level){
		return dao.find("select * from sys_platform_resource where level = ? order by orderList asc ",level);
	}


	//是否有子节点
	public boolean hasChildren(String pid){
		List<PlatformResource> lbr = dao.find("select * from sys_platform_resource t where t.parent_id = ? order by orderList asc ",pid);
		if(lbr!=null&&lbr.size()>0){
			return true;
		}else{
			return false;
		}
	}

	public List<PlatformResource> getByPid(String pid, String uid){
		if(StrKit.notBlank(pid)){
			return dao.find("select * from sys_platform_resource where 1=1 and parent_id = ? order by orderList asc",pid);
		}
		return dao.find("select * from sys_platform_resource where 1=1 and parent_id is null and level!='0' order by orderList asc");
	}
	public List<PlatformResource> getByPidOrCid(String pid, String uid){
		if(StrKit.notBlank(pid)){
			System.out.println("select * from sys_platform_resource where 1=1 and path like '%"+pid+"%' and path!=?");
			return dao.find("select * from sys_platform_resource where 1=1 and path like '%"+pid+"%' and path!=?",pid);
		}

		return dao.find("select * from sys_platform_resource where 1=1  and parent_id is null");
	}
	
	/**
	 * 获得所有资源
	 * @return
	 */
	public List<PlatformResource> getAll(){
		return dao.find("select * from sys_platform_resource");
	}

	/**
	 * 检查资源名是否存在
	 * @param 
	 * @return
	 */
	public boolean checkName(String name) {
		return dao.findFirst("select name from sys_platform_resource where name = ? limit 1", name) == null;
	}
	
	/**
	 * 检查资源值是否存在
	 * @param 
	 * @return
	 */
	public boolean checkValue(String value) {
		return dao.findFirst("select value from sys_platform_resource where value = ? limit 1", value) == null;
	}

	public static void main(String[] args) {
		System.out.println(StrKit.getRandomUUID());
	}
}
